#include "stdlib.hpp"
#include <purewell/types.hpp>
#include <purewell/pairsocket.hpp>

namespace purewell
{

PWPairSocket::PWPairSocket()
{
}

PWPairSocket::~PWPairSocket()
{
}

bool
PWPairSocket::initialize(const char* name, int flags, int omode)
{
	int fd[2];
	if ( 0 != socketpair(AF_LOCAL, SOCK_STREAM, 0, fd) ) return false;

	if ( !attach2(fd) )
	{
		::close(fd[0]);
		::close(fd[1]);
		return false;
	}

	return true;
}

ssize_t
PWPairSocket::receive(void* buf, size_t blen, int flag, size_t idx)
{
	return ::recv((idx == 0 ? m_fd : m_fd2 ), buf, blen, flag);
}

ssize_t
PWPairSocket::send(const void* buf, size_t blen, int flag, size_t idx)
{
	return ::send((idx == 0 ? m_fd : m_fd2 ), buf, blen, flag);
}

};//namespace purewell

